package com.code.user.config;

import com.code.common.model.ResponseCode;
import com.code.common.model.ResultVO;
import com.code.common.utils.HelperUtil;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: Yan
 * @date: 2023/4/20
 */
@Component
public class AuthenticationEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        ResultVO<Object> exp = ResultVO.ERROR(ResponseCode.SERVER_ERROR);

        if (e instanceof InsufficientAuthenticationException) {
            exp = ResultVO.ERROR(ResponseCode.NO_ACCESS);
        }

        HelperUtil.httpOutput(response, exp);
    }
}
